#include "EasyTrigger.h"

EasyTrigger g_pEasyTrigger;

bool EasyTrigger::DoTrigger( CUserCmd* Cmd )
{
	SD_Tools::Vector Forward;

	void* a;

	int LocalPlayerIndex = g_pInterfaces.g_pEngine->GetLocalPlayer();
	Entity* pMe = g_pInterfaces.g_pEntList->GetClientEntity(LocalPlayerIndex);

	JUNK_CODE;
	if (!pMe)
	{
		return false;
	}

	JUNK_CODE;
	a = pMe->GetBaseEntity();

	if (!a)
	{
		return false;
	}

	CTraceFilterSkipTwoEntities tracefilter(a, NULL);

	SD_Tools::Angle ViewAngle = Cmd->viewangles;
	JUNK_CODE;

	if(PredictRecoil)
	{
		ViewAngle.x += pMe->getRecoilVec()->x*1.92f;
		ViewAngle.y += pMe->getRecoilVec()->y*1.92f;
	}

	ViewAngle.ToVectors_s(&Forward, NULL, NULL);

	Forward.Multiply(8192.0f);

	SD_Tools::Vector EyePos = pMe->GetEyePosition();

	Forward.Add(EyePos);

	JUNK_CODE;
	player_info_t pInfo;

	//////////////////////////////////////////////////////////////////////////

	int MaxEntities = g_pInterfaces.g_pEntList->GetHighestEntityIndex();
	player_info_t pinfo;
	SD_Tools::Vector RealHead;
	JUNK_CODE;
	SD_Tools::Vector m_Eye = pMe->GetEyePosition();
	SD_Tools::Vector CurrentVec = SD_Tools::MathHelper::SubVector(Forward, m_Eye);
	JUNK_CODE;
	SD_Tools::Vector DesiredVec;

	for(int i = 1; i < MaxEntities; ++i)
	{
		Entity* baseEntity = g_pInterfaces.g_pEntList->GetClientEntity(i);

		if(i == LocalPlayerIndex || baseEntity == NULL || baseEntity->dormant() || baseEntity == pMe || *baseEntity->lifeState() != 0 /*LIFE_ALIVE*/ || !g_pInterfaces.g_pEngine->GetPlayerInfo(i, &pinfo) || *baseEntity->iTeamNum() == *pMe->iTeamNum() || !baseEntity->GetMidHead(&RealHead) || !GameUtilities::IsVisible(m_Eye, RealHead, pMe, baseEntity))
			continue;

		JUNK_CODE;
		float Range = GameUtilities::GetMaxRange(m_Eye, RealHead);

		DesiredVec = SD_Tools::MathHelper::SubVector(m_Eye, RealHead);

		float CurRange = GameUtilities::GetAngleBetween(CurrentVec, DesiredVec);

		if (180.0f - CurRange < Range)
		{
			return true;
		}
	}

	//////////////////////////////////////////////////////////////////////////

	Trace_t Trace;
	Ray_t Ray;

	Ray.Init(EyePos, Forward);
	JUNK_CODE;

	g_pInterfaces.g_pEngineTrace->TraceRay(Ray, MASK_HITBOX, &tracefilter, &Trace);

	Entity* HitEnt = *Trace.HitEntity();
	JUNK_CODE;

	if (!HitEnt || HitEnt->dormant() || *HitEnt->iHealth() <= 0 || !g_pInterfaces.g_pEngine->GetPlayerInfo(HitEnt->GetIndex(), &pInfo) || *HitEnt->iTeamNum() == *pMe->iTeamNum() || Trace.hitbox <= 0)
	{
		return false;
	}
	if (HeadOnly)
	{
		if (Trace.hitbox != 12)
		{
			JUNK_CODE;
			return false;
		}
	}

	return true;
}

void EasyTrigger::Init()
{
	this->Active = true;
	this->HeadOnly = true;
	this->PredictRecoil = true;
}

void EasyTrigger::Enable()
{
	Active = true;
}

void EasyTrigger::Disable()
{
	Active = false;
}

void EasyTrigger::Set( bool Active )
{
	this->Active = Active;
}

void EasyTrigger::SetHeadOnly( bool Active )
{
	this->HeadOnly = Active;
}

void EasyTrigger::Compute( CUserCmd* Cmd )
{
	if (Active)
	{
		if (GameUtilities::HasGun() && DoTrigger(Cmd))
		{
			Cmd->buttons |= IN_ATTACK;
		}
	}
}
